/*
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */
package com.sun.dtv.lwuit.painter;

import com.sun.dtv.lwuit.Form;
import com.sun.dtv.lwuit.Graphics;
import com.sun.dtv.lwuit.Painter;
import com.sun.dtv.lwuit.geom.Rectangle;

/**
 * A painter chain allows us to chain together several painters to provide a
 * "layer" effect where each painter only draws one element.
 *
 * @author Shai Almog
 */
public class PainterChain implements Painter {
    private Painter[] chain;
    
    /**
     * Create a new painter chain which will paint all of the elements in the chain
     * in sequence from 0 to the last element
     */
    public PainterChain(Painter[] chain) {
        this.chain = chain;
    }
    
    /**
     * Creates a new chain based on the existing chain with the new element added
     * at the end
     * 
     * @param p new painter
     * @return new chain element
     */
    public PainterChain addPainter(Painter p) {
        Painter[] newChain = new Painter[chain.length + 1];
        System.arraycopy(chain, 0, newChain, 0, chain.length);
        newChain[chain.length] = p;
        return new PainterChain(newChain);
    }
    
    /**
     * Creates a new chain based on the existing chain with the new element added
     * at the beginning
     * 
     * @param p new painter
     * @return new chain element
     */
    public PainterChain prependPainter(Painter p) {
        Painter[] newChain = new Painter[chain.length + 1];
        System.arraycopy(chain, 1, newChain, 0, chain.length);
        newChain[0] = p;
        return new PainterChain(newChain);
    }
    
    /**
     * @inheritDoc
     */
    public void paint(Graphics g, Rectangle rect) {
        for(int iter = 0 ; iter < chain.length ;  iter++) {
            chain[iter].paint(g, rect);
        }
    }

    /**
     * Installs a glass pane on the given form making sure to make it a painter
     * chain only if required by existing painter
     * 
     * @param f form on which to install the chain
     * @param p painter to install
     */
    public static void installGlassPane(Form f, Painter p) {
        Painter existing = f.getGlassPane();
        if(existing == null) {
            f.setGlassPane(p);
            return;
        }
        if(existing instanceof PainterChain) {
            ((PainterChain)existing).addPainter(p);
        } else {
            PainterChain pc = new PainterChain(new Painter[] {existing, p});
            f.setGlassPane(pc);
        }
    }
}
